package com.bfxy.lock.optimistic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class OptimisticTest {

    private final AtomicInteger i = new AtomicInteger(0);

    public static void main(String[] args) {
        OptimisticTest optimisticTest = new OptimisticTest();
        ExecutorService es = Executors.newFixedThreadPool(50);
        CountDownLatch cdl = new CountDownLatch(5000);
        for (int i = 0; i < 5000; i++){
            es.execute(()->{
                optimisticTest.i.incrementAndGet();
                cdl.countDown();
            });
        }
        es.shutdown();
        try {
            cdl.await();
            System.out.println("执行完成后，i="+ optimisticTest.i); // `incrementAndGet()`方法采用了CAS机制，也就是说使用了乐观锁
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
